/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow.ui.fragment.home;

import me.yokeyword.sample.ResourceTable;
import me.yokeyword.sample.demo_flow.base.MySupportFraction;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.utils.PacMap;


public class CycleFraction extends MySupportFraction {
    private static final String ARG_NUMBER = "arg_number";
    private Text mTvName;
    private Button mBtnNextWithFinish, mBtnNext;

    private int mNumber;

    public static CycleFraction newInstance(int number) {
        CycleFraction fraction = new CycleFraction();
        PacMap args = new PacMap();
        args.putIntValue(ARG_NUMBER, number);
        fraction.setArguments(args);
        return fraction;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_layout_fraction_cycle, container, false);
        initComponent(component);
        return component;
    }

    private void initComponent(Component component) {
        PacMap bundle = getArguments();
        if (bundle != null) {
            mNumber = bundle.getIntValue(ARG_NUMBER);
        }

        String title = "CycleFraction " + mNumber;

        mTvName = (Text) component.findComponentById(ResourceTable.Id_tv_name);
        mTvName.setText(title);
        mBtnNext = (Button) component.findComponentById(ResourceTable.Id_btn_next);
        mBtnNextWithFinish = (Button) component.findComponentById(ResourceTable.Id_btn_next_with_finish);
        mBtnNext.setClickedListener(component1 -> start(CycleFraction.newInstance(mNumber + 1)));
        mBtnNextWithFinish.setClickedListener(component12 -> startWithPop(CycleFraction.newInstance(mNumber + 1)));
    }
}
